package com.example.nacos.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 驼峰字符串转为下划线字符串
 */
public class CamelToUnderscore {
    public static String camelToUnderscore(String str) {
        if (str == null || str.isEmpty()) {
            return "";
        }

        Pattern pattern = Pattern.compile("[a-z]");
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}
